<?php
namespace app\admin\controller;

use app\common\model\admin\AccountModel;
use app\common\model\admin\ApplyModel;
use app\common\model\index\UserModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\admin\model\SystemUser;


/**
 * 申请账号管理
 * @package app\admin\controller
 * @author wangqinghui
 * @DateTime 2024-09-11 13:58:44
 */
class Apply extends Controller
{

    /**
     * 申请账号列表
     * @auth true
     * @menu true
     * @author Lihongqi
     * @DateTime 2024-09-11 14:56:28
     * @return void
     */
    public function index ()
    {
        ApplyModel::mQuery()->layTable(function () {

            $this->title = lang('账号申请');
            $this->status_arr = ApplyModel::getStatus();
        }, function (QueryHelper $query) {
            // 标记为学校账户
            $this->account_module = AccountModel::MODULE_SCHOOLS;
            // 如果有  名字  手机号   序号   专业   院系 等 用户搜索， 先搜索 用户表 获得id
            $params = $this->request->get();

            $map = [];
            empty($params['true_name'])  ||  $map[] = ['true_name', 'like', $params['true_name']];
            empty($params['mobile'])     ||  $map[] = ['mobile', '=', $params['mobile']];
            empty($params['job_number']) ||  $map[] = ['job_number', '=', $params['mobile']];

            if ( !empty($map) ) {

                $userids = UserModel::mk()->where($map)->column('id');

                empty($userids) && $userids = [0];

                $query->whereIn('index_user_id', $userids);
            }

            // 数据列表搜索过滤
            $query->with('user')->equal('account_id,status')->dateBetween('create_at');

        });
    }

    protected function _index_page_filter (&$data)
    {

        $account_name = AccountModel::mk()->column('title', 'id');

        $sex_arr       = UserModel::getSexArr();
        $background    = UserModel::getBackgroundArr();
        foreach ($data as &$item) {

            $item['account_name']    = $account_name[$item['account_id']] ?? ' - ';
            $item['avatar']          = $item['user']['avatar'] ?? '';
            $item['index_user_name'] = $item['user']['true_name'] ?? '';
            $item['mobile']          = $item['user']['mobile'] ?? '';
            $item['sex_text']        = isset($item['user']['sex']) ? ($sex_arr[$item['user']['sex']] ?? '') : '';
            $item['job_number']      = $item['user']['job_number'] ?? '';
            $item['job_depart']      = $item['user']['job_depart'] ?? '';
            $item['major']           = $item['user']['major'] ?? '';
            $item['edu_background_text'] = isset($item['user']['edu_background']) ? ($background[$item['user']['edu_background']] ?? '') : '';

        }
    }

    /**
     * 同意
     *
     * @auth true
     * @return void
     */
    public function agree ()
    {
        $id        = $this->request->post('id');
        $authorize = $this->request->post('authorize');

        $account_apply = ApplyModel::mk()->where('id', $id)->find();
        // 检测一下登陆账号是否 重复了
        $map = ['username' => $account_apply['login_account'], 'is_deleted' => 0];
        if (SystemUser::mk()->where($map)->count() > 0) {
            $this->error(lang('账号已经存在，请使用其它账号')."！");
        }

        $account_apply->status =  ApplyModel::STATUS_AGREE;
        $account_apply->check_at = date('Y-m-d H:i:s');
        $account_apply->save();


        // 获得申请用户信息
        $index_user_model = UserModel::mk()->where('id', $account_apply['index_user_id'])->find();
        // 写入用户表
        $systemUser = new SystemUser();
        $systemUser->usertype   = 'account';
        $systemUser->username   = $account_apply->login_account;
        $systemUser->password   = md5($account_apply->password);
        $systemUser->nickname   = $index_user_model->true_name;
        $systemUser->headimg    = $index_user_model->avatar;
        $systemUser->authorize  = $authorize;
        $systemUser->account_id = $account_apply->account_id;

        $systemUser->save();

        $this->success('成功');
    }

    /**
     * 拒绝
     *
     * @auth true
     * @return void
     */
    public function refuse ()
    {

        $this->status = ApplyModel::STATUS_REFUSE;
        ApplyModel::mForm();
    }



}